﻿//-----------------------------------------------------------------------
// <copyright file="GetPublicKeyToken.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary></summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Tools.StrongName
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Globalization;
    using System.Reflection;
    using Microsoft.Win32;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Tasks;
    using System.Security.Cryptography;

    public sealed class GetPublicKeyToken : TaskBase
    {
        private string keyfile;
        private string publicKeyToken;

        /// <summary>
        /// Initializes a new instance of the GetPublicKeyToken class.
        /// </summary>
        public GetPublicKeyToken()
        {
        }

        /// <summary>The path to the public snk file to extract from</summary>
        [Required]
        public string KeyFile
        {
            get { return this.keyfile; }
            set { this.keyfile = value; }
        }

        /// <summary>The public key token</summary>
        [Output]
        public string PublicKeyToken
        {
            get { return this.publicKeyToken; }
            set { this.publicKeyToken = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            byte[] buffer1;
            StrongNameKeyPair keyPair = null;
            byte[] publicKey = null;

            using (FileStream stream1 = new FileStream(this.KeyFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                buffer1 = new byte[(int)stream1.Length];
                stream1.Read(buffer1, 0, (int)stream1.Length);
            }

            StrongNameKeyPair pair1 = new StrongNameKeyPair(buffer1);
            try
            {
                publicKey = pair1.PublicKey;
                keyPair = pair1;
            }
            catch (ArgumentException)
            {
                publicKey = buffer1;
            }

            SHA1Managed sha1Managed = new SHA1Managed();
            byte[] hash = sha1Managed.ComputeHash(publicKey);

            byte[] publicKeyToken = new byte[8];
            Array.Copy(hash, hash.Length - publicKeyToken.Length, publicKeyToken, 0, publicKeyToken.Length);
            Array.Reverse(publicKeyToken, 0, publicKeyToken.Length);

            System.Text.StringBuilder sb = new System.Text.StringBuilder(16);
            for (int i = 0; i < publicKeyToken.Length; i++)
            {
                sb.Append(publicKeyToken[i].ToString("x2"));
            }
            this.PublicKeyToken = sb.ToString();
        }
    }
}
